import LoopListCell, { ShowNode } from "../Script/LoopListCell";
import RenderAlternative from "../Script/RenderAlternative";
const { ccclass, property } = cc._decorator;
export interface IData {
    size?: cc.Size,
    index: number,
    text: string
}

@ccclass
export class TestShowNode extends ShowNode<IData> {
    /** */
    @property(cc.Component)
    public renderAlternatives: cc.Component[] = [];
    /** */
    @property(cc.Label)
    public nameLb: cc.Label = null;
    /** */
    @property(cc.Label)
    public contentLb: cc.Label = null;
    /** */
    @property(cc.Node)
    public Button: cc.Node = null;
    /** */
    @property(cc.Label)
    public ButtonLb: cc.Label = null;

    data: IData;

    /**
     * 初始化显示
     * @param data 
     */
    initShow(data: IData, cell: LoopListCell<IData>, renderLayer: cc.Node) {
        if (renderLayer) {
            this.initRenders(renderLayer);
        }
        this.data = data;
        this.node.width = this.data.size.width;
        this.node.height = this.data.size.height;
        this.nameLb.string = "我是第" + this.data.index + "个";
        this.contentLb.string = "我是内容" + this.data.text;
        //这个是改变控件大小的事例
        if (data.index == 5 || data.index == 11) {
            //this.changeSize(cell);
        }
    }

    /**
     * 改变大小
     * @param cell 
     */
    changeSize(cell: LoopListCell<IData>) {
        cell.resize(cc.size(cell.size.width, 100 + (Math.random() * 400)));
        this.scheduleOnce(() => {
            this.changeSize(cell)
        }, 3 + Math.random() * 3)
    }

    hasInit: boolean = false;
    /**
     * 处理分层代理渲染
     * @param renderLayer 
     */
    initRenders(renderLayer: cc.Node) {
        if (!this.hasInit) {
            this.nameLb = this.nameLb.getComponent(RenderAlternative)?.init<cc.Label>(renderLayer);
            this.contentLb = this.contentLb.getComponent(RenderAlternative)?.init<cc.Label>(renderLayer);
            this.ButtonLb = this.ButtonLb.getComponent(RenderAlternative)?.init<cc.Label>(renderLayer);
            for (let index = 0; index < this.renderAlternatives.length; index++) {
                const element = this.renderAlternatives[index];
                element.getComponent(RenderAlternative)?.init(renderLayer)
            }
            this.hasInit = true;
        }
    }

    onRecircle() {
        //做释放操作
    }
}


